<?php

namespace app\admin\service;

use app\admin\model\BoxNotice;
use app\admin\model\Member;
use app\qsapi\model\BoxMemberInfo;

class BoxNoticeService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxNotice();
    }

    /**
     * Desc : getList
     * User : Steven
     * Date : 2024-07-10 14:57
     * @return array
     */
    public function getList(): array
    {
        $param = request()->param();
        // 查询条件
        $map = [];
        if (isset($param['id']) && !empty($param['id'])) {
            $map[] = ['a.id', '=', $param['id']];
        }
        if (isset($param['source']) && !empty($param['source'])) {
            $map[] = ['a.source', '=', $param['source']];
        }
        if (isset($param['title']) && !empty($param['title'])) {
            $map[] = ['a.title', 'like', '%'.$param['title'].'%'];
        }
        $map[] = ['a.mark','=', 1];
        $noticeModel = new BoxNotice();
        $list = $noticeModel->alias('a')
            ->leftJoin('cq_member b','a.member_id=b.id')
            ->field('a.*,b.nickname')
            ->where($map)
            ->order('a.id', 'desc')
            ->paginate($param['limit']);
        $data['code'] = 0;
        $data['msg'] = '操作成功';
        $data['data'] = $list->items() ?? [];
        $data['count'] = $list->total() ?? 0;
        return $data;
    }

    /**
     * @param $member_id
     * @param $title
     * @param $content
     * @param int $source
     * @param int $extend_id
     * @return array
     */
    public function sendNotice($member_id, $title, $content, int $source = 1, int $extend_id=0): array
    {
        //$member_id == -1  为全局推送消息
        if($member_id >= 0){
            //获取用户信息
            $member_info= BoxMemberInfo::where('member_id',$member_id)->find();
            //用户关闭站内信  不发送
            if(!$member_info || $member_info['is_push_notice'] ==0) return message('当前用户已关闭站内信功能,无法收到这条推送消息!', false);
        }
        $insert_id = $this->model->insertGetId([
            'member_id' => $member_id,
            'title' => $title,
            'content' => $content,
            'source' => $source,
            'create_time' => time(),
            'extend_id'=>$extend_id
        ]);
        return message('发送成功', true, $insert_id);
    }

    /**
     * Desc : getMemberInfo
     * User : Steven
     * Date : 2024-07-10 15:16
     * @return array
     */
    public function getMemberInfo(): array
    {
        $param = request()->param();
        $map = [];
        $where = [];
        if (isset($param['nickname']) && !empty($param['nickname'])) {
            $map[] = ['nickname', 'like', '%'.$param['nickname'].'%'];
        }
        if(isset($param['nickname']) && is_numeric($param['nickname'])){
            $where[] = ['id', '=', $param['nickname']];
        }
        $list = Member::field('id,nickname')->where($map)->whereOr($where)->order('id','desc')->paginate($param['limit'])->toArray();
        $page = $param['page'] ?? 1;
        $data = $list['data'];
        if($page == 1 && empty($param['nickname'])){
            array_unshift($data,['id'=>-1,'nickname'=>'全局消息']);
        }
        return array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,
            "count" => $list['total'],
        );
    }
}